<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>

        <el-col :span="4"
                style="float: right;"
                align="right">
          <el-button @click="openEditUI(null)"
                     type="primary"
                     icon="el-icon-plus"
                     circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="leaveList"
                stripe
                style="width: 100%">
        <el-table-column label="#"
                         width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1)*pageSize +index +1 -->
            {{scope.$index +1 }}
          </template>

        </el-table-column>

        <el-table-column prop="msg"
                         label="请假理由 "
                         width="180">
        </el-table-column>
        <el-table-column prop="type"
                         label="类型"
                         width="180">
        </el-table-column>
        <el-table-column prop="startTime"
                         label="开始时间">
        </el-table-column>
        <el-table-column prop="endTime"
                         label="结束时间">
        </el-table-column>
        <el-table-column prop="isPass"
                         label="审批状态"
                         width="180">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isPass == 0"
                    type="warning">未审</el-tag>
            <el-tag v-if="scope.row.isPass == 1"
                    type="success">通过</el-tag>
            <el-tag v-if="scope.row.isPass == 2"
                    type="danger">拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="180">
          <template slot-scope="scope">
            <el-button type="primary"
                       @click="openEditUI(scope.row.id)"
                       icon="el-icon-edit"
                       size="mini"
                       circle></el-button>
            <el-button type="danger"
                       @click="deleteLeave(scope.row)"
                       icon="el-icon-delete"
                       size="mini"
                       circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 用户信息编辑对话框 -->
    <el-dialog @close="clearForm"
               :title="title"
               :visible.sync="dialogFormVisible">
      <el-form :model="leaveForm"
               ref="leaveFormRef"
               :rules="rules">
        <el-form-item label="请假理由"
                      prop="msg"
                      :label-width="formLabelWidth">
          <el-input v-model="leaveForm.msg"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型"
                      prop="type"
                      :label-width="formLabelWidth">
          <el-select v-model="leaveForm.type"
                     placeholder="请选择">
            <el-option v-for="(item,index) in type"
                       :key="index"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间"
                      :label-width="formLabelWidth">
          <el-date-picker v-model="leaveForm.startTime"
                          type="datetime"
                          placeholder="选择开始时间"
                          value-format="yyyy-MM-dd 00:00">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间"
                      :label-width="formLabelWidth">
          <el-date-picker v-model="leaveForm.endTime"
                          type="datetime"
                          placeholder="选择结束时间"
                          value-format="yyyy-MM-dd 00:00">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveLeave">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
   
   <script>
import leaveApi from '@/api/leave'
import { getInfo } from '@/api/user'
export default {
  data () {
    return {
      userid: '',
      //   type类型
      type: ["事假", "病假", "公假"],
      formLabelWidth: '130px',
      leaveForm: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      leaveList: [],
      rules: {
        msg: [
          { required: true, min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        startTime: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    deleteLeave (leave) {
      this.$confirm(`您确认删除${leave.msg}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        leaveApi.deleteLeaveById(leave.id).then(respone => {
          this.$message({
            type: 'success',
            message: respone.message
          });
          this.getLeaveList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    saveLeave () {
      // 触发表单验证
      this.$refs.leaveFormRef.validate((valid) => {
        if (valid) {
          this.leaveForm.userId = this.userid
          // 把提交请求给后台 
          leaveApi.saveLeave(this.leaveForm).then((respone) => {
            // 成功提示
            this.$message({
              message: respone.message,
              type: 'success'
            });
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getLeaveList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    clearForm () {
      this.leaveForm = {}
      this.$refs.leaveFormRef.clearValidate();
    },
    openEditUI (id) {
      if (id == null) {
        this.title = "新增请假"
      } else {
        this.title = "修改请假数据"
        // 根据id查询用户数据
        leaveApi.getLeaveMsg(id).then(respone => {
          this.leaveForm = respone.data
        })
      }

      this.dialogFormVisible = true
    },

    getLeaveList () {
      getInfo().then(res => {
        this.userid = res.data.userid
        console.log(this.userid);
        leaveApi.getLeaveList(this.userid).then(res => {
          this.leaveList = res.data
        })
      })

    }
  },
  created () {
    this.getLeaveList()
  }
}
   </script>
   
   <style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 80%;
}
</style>